package net.lainiao.morethread.Chapter4.demo5;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by Administrator on 2017/3/3.
 */
public class Main {
    public static void main(String[] args) {
        ExecutorService executorService=(ExecutorService) Executors.newCachedThreadPool();
        List<Task> taskList=new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Task task=new Task("task"+i);
            taskList.add(task);
        }
        List<Future<Result>> resultList=null;
        try {
            resultList=executorService.invokeAll(taskList);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();

        System.out.println("Main: Printing the results");
        for (int i = 0; i < resultList.size(); i++) {
            Future<Result> future=resultList.get(i);
            try {
                Result result=future.get();
                System.out.println(result.getName()+" : "+result.getValue());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

    }
}
